package com.bienvenue.main.ui.activity.personal;

import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.bienvenue.main.R;
import com.bienvenue.main.base.BaseActivity;
import com.bienvenue.main.httpService.ResponseObserver;
import com.bienvenue.main.model.HttpBean;
import com.bienvenue.main.model.JobPostModel;
import com.bienvenue.main.model.ResumeInfoModel;
import com.bienvenue.main.model.SaveResumeModel;
import com.bienvenue.main.serviceApi.ServiceModel;
import com.bienvenue.main.ui.activity.enterprise.PostJobActivity;
import com.bienvenue.main.utils.SPUtils;
import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.bigkoo.pickerview.view.TimePickerView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 　　　　　　　 ┏┓       ┏┓+ +
 * 　　　　　　　┏┛┻━━━━━━━┛┻┓ + +
 * 　　　　　　　┃　　　　　　 ┃
 * 　　　　　　　┃　　　━　　　┃ ++ + + +
 * 　　　　　　 █████━█████  ┃+
 * 　　　　　　　┃　　　　　　 ┃ +
 * 　　　　　　　┃　　　┻　　　┃
 * 　　　　　　　┃　　　　　　 ┃ + +
 * 　　　　　　　┗━━┓　　　 ┏━┛
 * ┃　　  ┃
 * 　　　　　　　　　┃　　  ┃ + + + +
 * 　　　　　　　　　┃　　　┃　Code is far away from     bug with the animal protecting
 * 　　　　　　　　　┃　　　┃ + 　　　　         神兽保佑,代码无bug
 * 　　　　　　　　　┃　　　┃
 * 　　　　　　　　　┃　　　┃　　+
 * 　　　　　　　　　┃　 　 ┗━━━┓ + +
 * 　　　　　　　　　┃ 　　　　　┣┓
 * 　　　　　　　　　┃ 　　　　　┏┛
 * 　　　　　　　　　┗┓┓┏━━━┳┓┏┛ + + + +
 * 　　　　　　　　　 ┃┫┫　 ┃┫┫
 * 　　　　　　　　　 ┗┻┛　 ┗┻┛+ + + +
 *
 * @ProjectName: Bienvenue
 * @Package: com.bienvenue.main.ui.activity
 * @ClassName: EducationBackgroundActivity
 * @Description: 教育背景
 * @Author: Mr.Chen
 * @UpdateUser: Mr.Chen
 * @UpdateDate: 2020/9/16
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class EducationBackgroundActivity extends BaseActivity {
    @BindView(R.id.et_school_name)
    EditText etSchoolName;
    @BindView(R.id.tv_start_time)
    TextView tvStartTime;
    @BindView(R.id.tv_end_time)
    TextView tvEndTime;
    @BindView(R.id.tv_education)
    TextView tvEducation;
    @BindView(R.id.et_profession)
    EditText etProfession;
    @BindView(R.id.tv_delete_btn)
    TextView tvDeleteBtn;
    String userId, tvEducationId;
    int type;
    List<String> stringList = new ArrayList<>();
    ResumeInfoModel.DataBean.UResumeEducationBean bean;
    @Override
    protected void initData() {
        userId = SPUtils.getString("userId");
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
              type = bundle.getInt("type");
             bean = bundle.getParcelable("educationBackground");
            if (type == 1) {
                tvDeleteBtn.setVisibility(View.VISIBLE);
                etSchoolName.setText(bean.getSchoolname());
                String time = bean.getStartendtime();
                String startTime = time.substring(0, time.indexOf("~"));
                tvStartTime.setText(startTime);
                String endTime = time.substring(time.indexOf("~"));
                tvEndTime.setText(endTime);
                tvEducation.setText(bean.getEducation());
                etProfession.setText(bean.getMajor());
            }else{
                tvDeleteBtn.setVisibility(View.GONE);
            }
        }

    }

    @Override
    protected void initView() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_education_background;
    }

    @OnClick(R.id.iv_return_btn)
    void onFinish() {
        this.finish();
    }

    @OnClick(R.id.tv_start_time)
    void onStartTime() {
        Calendar selectedDate = Calendar.getInstance();//系统当前时间

        Calendar startDate = Calendar.getInstance();
        startDate.set(1927, 1, 1);
        Calendar endDate = Calendar.getInstance();
        endDate.set(2022, 12, 31);
        TimePickerView timePicker = new TimePickerBuilder(this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                tvStartTime.setText(getTime(date));
            }
        })
                .setTitleText("请选择开始时间")
                .setGravity(Gravity.BOTTOM)
                .setContentTextSize(18)
                .setTitleSize(20)
                .setDate(selectedDate)
                .setSubmitText("确定")
                .setCancelText("取消")
                .setRangDate(startDate, endDate)
                .build();
        timePicker.show();
    }

    @OnClick(R.id.tv_end_time)
    void onEndTime() {
        Calendar selectedDate = Calendar.getInstance();//系统当前时间

        Calendar startDate = Calendar.getInstance();
        startDate.set(1927, 1, 1);
        Calendar endDate = Calendar.getInstance();
        endDate.set(2022, 12, 31);
        TimePickerView timePicker = new TimePickerBuilder(this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                tvEndTime.setText(getTime(date));
            }
        })
                .setTitleText("请选择结束时间")
                .setGravity(Gravity.BOTTOM)
                .setContentTextSize(18)
                .setTitleSize(20)
                .setDate(selectedDate)
                .setSubmitText("确定")
                .setCancelText("取消")
                .setRangDate(startDate, endDate)
                .build();
        timePicker.show();
    }

    @OnClick(R.id.tv_education)
    void onEducation() {
        ServiceModel.getServiceModel().getListByTypeEducate(new ResponseObserver<JobPostModel>() {
            @Override
            public void onSuccess(JobPostModel response) {
                if (response.getCode() == 200) {

                    if (stringList != null && stringList.size() > 0) {
                        stringList.clear();
                    }
                    for (int i = 0; i < response.getData().size(); i++) {
                        stringList.add(response.getData().get(i).getVal());
                    }
                    OptionsPickerView pvOptions = new OptionsPickerBuilder(EducationBackgroundActivity.this, new OnOptionsSelectListener() {
                        @Override
                        public void onOptionsSelect(int options1, int options2, int options3, View v) {
                            //返回的分别是三个级别的选中位置
                            if (response.getData().get(options1).getVal().equals(stringList.get(options1))) {
                                tvEducationId = response.getData().get(options1).getId();
                            }
                            tvEducation.setText(stringList.get(options1));
                        }
                    })
                            .setTitleText("请选择学历")
                            .setDividerColor(Color.BLACK)
                            .setTextColorCenter(Color.BLACK) //设置选中项文字颜色
                            .setContentTextSize(20)
                            .build();
                    pvOptions.setPicker(stringList);
                    pvOptions.show();
                } else {
                    showToast(response.getMsg());
                }
            }

            @Override
            public void onFail(String message) {
                Log.e("TAG", message);
            }
        });
    }

    @OnClick(R.id.tv_save_btn)
    void onSave() {
        if (TextUtils.isEmpty(etSchoolName.getText().toString().trim()) && "".equals(etSchoolName.getText().toString().trim())) {
            showToast("请输入学校名称");
            return;
        } else if ("开始时间".equals(tvStartTime.getText().toString().trim())) {
            showToast("请选择开始时间");
            return;
        } else if ("结束时间".equals(tvEndTime.getText().toString().trim())) {
            showToast("请选择结束时间");
            return;
        } else if ("请选择学历".equals(tvEducation.getText().toString().trim())) {
            showToast("请选择学历");
            return;
        } else if (TextUtils.isEmpty(etProfession.getText().toString().trim()) && etProfession.getText().toString().trim().equals("")) {
            showToast("请输入专业");
            return;
        } else if (TextUtils.isEmpty(userId) && userId.equals("")) {
            showToast("信息错误，请重新登录");
        }
        SaveResumeModel.EducationModel model = new SaveResumeModel.EducationModel();
        model.setEducation(tvEducation.getText().toString().trim());
        model.setMajor(etProfession.getText().toString().trim());
        model.setSchoolname(etSchoolName.getText().toString().trim());
        model.setStartendtime(tvStartTime.getText().toString().trim() + "~" + tvEndTime.getText().toString().trim());
        model.setUserid(userId);
        model.setId(bean.getId());

        if (type == 1) {
            userId = bean.getId();
        } else {
            userId = SPUtils.getString("userId");
        }
        ServiceModel.getServiceModel().onSaveResumeEducation(model, new ResponseObserver<HttpBean>() {
            @Override
            public void onSuccess(HttpBean response) {
                if (response.getCode() == 200) {
                    showToast(response.getMsg());
                    EducationBackgroundActivity.this.finish();
                } else {
                    showToast(response.getMsg());
                }
            }

            @Override
            public void onFail(String message) {
                showLog(message);
            }
        });

    }
    @OnClick(R.id.tv_delete_btn)
    void onDelete(){
        ServiceModel.getServiceModel().deleteEducation(bean.getId(), new ResponseObserver<HttpBean>() {
            @Override
            public void onSuccess(HttpBean response) {
                if (response.getCode()==200){
                    showToast(response.getMsg());
                    EducationBackgroundActivity.this.finish();
                }else{
                    showToast(response.getMsg());
                }
            }

            @Override
            public void onFail(String message) {
                showLog(message);

            }
        });

    }
}
